package com.laguille.games.tetris.applet;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;

import javax.swing.JApplet;
import javax.swing.Timer;
import javax.swing.UIManager;

import com.laguille.games.tetris.model.Field;
import com.laguille.games.tetris.model.Model;
import com.laguille.games.tetris.model.Tetromino.TetrominoBlock;
import com.laguille.games.tetris.model.Tetromino.impl.IPiece;
import com.laguille.games.tetris.model.Tetromino.impl.TPiece;

public class TetrisApplet extends JApplet implements ActionListener {

	Model model = null;
	public final int GRID_X_OFFSET = 100; 
	public final int GRID_Y_OFFSET = 100;
	public final int GRID_WIDTH = 100; 
	public final int GRID_HEIGHT = 200;
	public final int SMALL_WINDOW_X_OFFSET = 400; 
	public final int SMALL_WINDOW_Y_OFFSET = 100;
	public final int SMALL_WINDOW_WIDTH = 70; 
	public final int SMALL_WINDOW_HEIGHT = 70;
	
	public final int MULT_FACTOR = 10;
	
	public int INITIAL_DELAY = 100;
	
	private Timer timer;
    public void init() {
        /* Turn off metal's use of bold fonts */
        UIManager.put("swing.boldMetal", Boolean.FALSE);
        model = new Model();
        setSize(500, 500);
        addKeyListener(new UserInputListener(model));
        //Execute a job on the event-dispatching thread; creating this applet's GUI.
        //Set up the timer that will perform the animation.
        timer = new Timer(INITIAL_DELAY, this);
        timer.setInitialDelay(INITIAL_DELAY);
        timer.setRepeats(true);
        timer.setCoalesce(false);
        timer.start(); //Start the animation.
    }
    
    private Rectangle2D square =
        new Rectangle2D.Double(GRID_X_OFFSET, GRID_Y_OFFSET, GRID_WIDTH, GRID_HEIGHT);
    
    private Rectangle2D smallWindow =
        new Rectangle2D.Double(SMALL_WINDOW_X_OFFSET, SMALL_WINDOW_Y_OFFSET, SMALL_WINDOW_WIDTH, SMALL_WINDOW_HEIGHT);
        
    private int width = MULT_FACTOR;
	private int height = MULT_FACTOR;
    @Override
    public void paint(Graphics g) {
    	Graphics2D g2d = (Graphics2D)g;
    	
    	// clean & redraw the matrix
    	g2d.clearRect(GRID_X_OFFSET, GRID_Y_OFFSET, GRID_WIDTH, GRID_HEIGHT);
    	g2d.draw(square);
        g2d.setColor(Color.RED);
    	for (TetrominoBlock b : model.getField().getAllBlocks())
        {
        	double x = GRID_X_OFFSET + b.getCoordinates().getX() * MULT_FACTOR;
        	double y = GRID_Y_OFFSET + (Field.HEIGHT - 1 - b.getCoordinates().getY()) * MULT_FACTOR;
        	Rectangle2D.Double piece = new Rectangle2D.Double(x, y, width, height);
//        	System.out.println(model.getCurrentPiece().getClass().getName() + " " + b + " X="+x+" Y="+y+" W="+width+" H=" + height);
        	g2d.draw(piece);
		}
    	
    	// clean & redraw the next piece
    	g2d.clearRect(SMALL_WINDOW_X_OFFSET, SMALL_WINDOW_Y_OFFSET, SMALL_WINDOW_WIDTH, SMALL_WINDOW_HEIGHT);
        g2d.setColor(Color.BLACK);
    	g2d.draw(smallWindow);
    	
    	for (TetrominoBlock b : model.getNextPiece().getBlocks())
        {
        	double x = SMALL_WINDOW_X_OFFSET + (b.getCoordinates().getX() - Field.WIDTH / 2) * MULT_FACTOR + SMALL_WINDOW_WIDTH / 2; 
        	double y = SMALL_WINDOW_Y_OFFSET + (Field.HEIGHT - 2 - b.getCoordinates().getY()) * MULT_FACTOR + SMALL_WINDOW_HEIGHT / 2;
        	if (model.getNextPiece() instanceof IPiece)
        	{
        		y = y - height;
        		x = x + width;
        	}
        	Rectangle2D.Double piece = new Rectangle2D.Double(x, y, width, height);
        	g2d.draw(piece);
		}
    }

    private int counter = 0;
	public void actionPerformed(ActionEvent e) {
		model.nextStep();
		repaint();
//		if (counter++ % 3 == 0)
//		{
//			timer.setDelay(timer.getDelay() / 2);
//		}
	}
    
}
